package com.stox.shared.charting.model;

import java.util.Collections;
import java.util.List;

import com.stox.shared.core.model.Bar;

public class AggregateBar extends Bar {

	private final List<Bar> bars;
	private final boolean up;

	public AggregateBar(final List<Bar> bars, final boolean up) {
		this.bars = bars;
		this.up = up;
		if (!bars.isEmpty()) {
			setClose(bars.get(0).getClose());
			setOpen(bars.get(bars.size() - 1).getOpen());
			setDate(bars.get(0).getDate());
			setHigh(Double.MIN_VALUE);
			setLow(Double.MAX_VALUE);
			for (int index = 0; index < bars.size(); index++) {
				final Bar bar = bars.get(index);
				setHigh(Math.max(getHigh(), bar.getHigh()));
				setLow(Math.min(getLow(), bar.getLow()));
				setVolume(getVolume() + bar.getVolume());
			}
		}
	}

	public boolean isUp() {
		return up;
	}

	public List<Bar> getBars() {
		return Collections.unmodifiableList(bars);
	}
}
